(function ($) {
    $.messager = {
		closeTime: null,
		init: function(fn){
			var nowDate = new Date(),millisecond = 0;
			if(this.closeTime != null){
				millisecond = 200 - nowDate.getTime() + this.closeTime.getTime();
				this.closeTime = null;
				if(millisecond < 0){
					millisecond = 0;
				}
			}
			var timeout = setTimeout(function(){
				fn();
				clearTimeout(timeout);
			},millisecond);
		},
        alert: function (title, msg, type, fn, closeTimer) {
			if(typeof type == 'function'){
				fn = type;
				type = '';
			}
			var timer = 10000;
			if(type == "success" || !type) {
				msg = "1秒后自动关闭...";
				timer = 1000;
				closeTimer = true;
			}
			var that = this;
			this.init(function(){
				top.sweetAlert({
				    title: title,
				    type: type || "success",
				    text: closeTimer?msg:(msg || "10秒后自动关闭..."),
				    timer: closeTimer?timer:10000,
				    confirmButtonText: "确认"
				}, function (res) {
					that.closeTime = new Date();
					if (fn && typeof fn === "function") {
						fn(res);
					}
				    top.sweetAlert.close();
				});
			});
            return top.sweetAlert;
        },

        confirm: function (title, msg, type, fn) {
			var that = this;
			if(typeof type == 'function'){
				fn = type;
				type = '';
			}
			this.init(function(){
				top.sweetAlert({
				    title: title,
				    type: type || "warning",
				    text: msg,
				    showConfirmButton: true,
				    confirmButtonText: "确认",
				    confirmButtonClass: "btn-info",
				    closeOnConfirm: true,
				    allowOutsideClick: true,
				    showCancelButton: true,
				    cancelButtonText: "取消",
				    cancelButtonClass: "btn-default"
				}, function (res) {
					that.closeTime = new Date();
				    if (fn && typeof fn === "function") {
				    	if(fn.length) fn(res);
				    	else if(res) fn();
				    }
				    top.sweetAlert.close();
				});
			});
            return top.sweetAlert;
        },

        prompt: function (title, msg, fn) {

        }
    };

    $.messager.defaults = $.extend({}, {
        ok: 'Ok',
        cancel: 'Cancel',
        width: 300,
        height: 'auto',
        minHeight: 150,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: false,
        fn: function () {
        }
    });

})(jQuery);
